import { _decorator, Vec3, Node, Component, Contact2DType, Collider2D, randomRange, tween, randomRangeInt } from 'cc';
import { EGroup, EnemyManager } from '../EnemyManager';
import ObjectPoolManager from '../Core/ObjectPoolManager';
import { Core } from '../Core/Core';

const { ccclass } = _decorator;

@ccclass('Arrow')
export class Arrow extends Component {

    moveSpeed: number = 800;



    protected onEnable(): void {
        let collider = this.node.getComponent(Collider2D);
        collider?.on(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    }

    protected onDisable(): void {
        let collider = this.node.getComponent(Collider2D);
        collider?.off(Contact2DType.BEGIN_CONTACT, this.onBeginContact, this);
    }

    onBeginContact(self: Collider2D, other: Collider2D) {
        if (other.group === EGroup.ENEMY) {
            const em = other.node.getComponent(EnemyManager);
            if (em) {
                em.hurt(em.hp/2, this.node);

            }
        }

        
    }

    update(deltaTime: number) {
        this.move(deltaTime);
    }


    move(dt: number) {
        //console.log("释放将军技能万箭齐发this.node.worldPosition",this.node.worldPosition);

            if (
                this.node.worldPosition.x > Core.Instance.screenBounds.maxX || // 
                this.node.worldPosition.x < Core.Instance.screenBounds.minX || // 0
                this.node.worldPosition.y > Core.Instance.screenBounds.maxY || // 
                this.node.worldPosition.y < Core.Instance.screenBounds.minY // 0
            ) {
                ObjectPoolManager.Instance.ret(this.node);
                return;
            }

                this.node.setPosition(
                    this.node.position.x +randomRangeInt(-50,50) * dt,
                    this.node.position.y + (this.moveSpeed+randomRangeInt(-100,100)) * dt
                );
            
        
    }


}